package com.bjpowernode.money.web;

import com.alibaba.dubbo.config.annotation.Reference;
import com.bjpowernode.money.Constant;
import com.bjpowernode.money.model.LoanInfo;
import com.bjpowernode.money.service.BidInfoService;
import com.bjpowernode.money.service.LoanInfoService;
import com.bjpowernode.money.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Controller
public class IndexController {

    @Reference(interfaceClass = LoanInfoService.class,version = "1.0.0",timeout = 20000,check = false)
    LoanInfoService loanInfoService;

    @Reference(interfaceClass = UserService.class,version = "1.0.0",timeout = 20000,check = false)
    UserService userService;

    @Reference(interfaceClass = BidInfoService.class,version = "1.0.0",timeout = 20000,check = false)
    BidInfoService bidInfoService;

    @RequestMapping("/index")
    public String index(Model model){

        //首页 历史年化收益率
        Double loanInfoHistoryRateAvg =  loanInfoService.queryLoanInfoHistoryRateAvg();
        model.addAttribute(Constant.LOAN_INFO_HISTORY_RATE_AVG,loanInfoHistoryRateAvg);

        //首页 平台用户数
        Long userCount = userService.queryUserCount();
        model.addAttribute(Constant.USER_COUNT,userCount);

        //首页 累计成交额
        Double bidInfoMoneySum = bidInfoService.queryBidInfoMoneySum();
        model.addAttribute(Constant.BID_INFO_MONEY_SUM,bidInfoMoneySum);

        /**
         *  要分别在首页显示新手宝、季度宝、散标的数据情况
         *  要在数据中根据类型的不同分别查出来，每个类型在首页显示的个数也不同，
         *  所以sql语句需要limit分页，根据需求
         *  要传入三个参数，三个参数以上可以装在一个Map集合中
         */
        //新手宝(需要传入三个参数金融类型、分页起始和终止页码)
        Map<String ,Object> parasMap = new HashMap<>();
        parasMap.put("ptype",0);
        parasMap.put("start",0);
        parasMap.put("content",1);

        List<LoanInfo> loanInfos_X = loanInfoService.queryLoanInfosByTypeAndNumber(parasMap);
        model.addAttribute(Constant.LOAN_INFOS_X,loanInfos_X);

        //季度宝  不用在重新创建集合了，会覆盖
        parasMap.put("ptype",1);
        parasMap.put("start",0);
        parasMap.put("content",4);

        List<LoanInfo> loanInfos_Y = loanInfoService.queryLoanInfosByTypeAndNumber(parasMap);
        model.addAttribute(Constant.LOAN_INFOS_Y,loanInfos_Y);

        //散标
        parasMap.put("ptype",2);
        parasMap.put("start",0);
        parasMap.put("content",8);

        List<LoanInfo> loanInfos_S = loanInfoService.queryLoanInfosByTypeAndNumber(parasMap);
        model.addAttribute(Constant.LOAN_INFOS_S,loanInfos_S);

        return "index";
    }



}
